package com.kirk.cts.ui.widget;

import com.kirk.cts.core.utils.ImageManager;
import com.kirk.cts.ui.utils.SwingUtils;

import javax.swing.*;
import java.awt.*;

/**
 * <p> JSplash.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-08-29 22:32
 * @Description 加载进度条控件
 */
public class JSplash extends JWindow implements Splash {
    private static final long serialVersionUID = 1L;

    /**
     * Progress bar to use in the splash screen.
     * 在初始屏幕中使用的进度条。
     */
    private JProgressBar m_progress = null;

    /**
     * Check for whether to use the progress bar or not.
     * 检查是否使用进度条。
     */
    private boolean m_progressBar = false;

    /**
     * Check for whether to use progress bar messages or not.
     * 检查是否使用进度条消息。
     */
    private boolean m_progressBarMessages = false;

    /**
     * Check for whether to use precentage values or not.
     * 检查是否使用预值。
     */
    private boolean m_progressBarPercent = false;

    private int total_progress = 0;

    private static JSplash instance = new JSplash();

    // public JSplash(URL url,
    // boolean progress, boolean messages, boolean percent,
    // String versionString, Font versionStringFont, Color versionStringColor)

    public static JSplash getInstance() {
        return instance;
    }

    private JSplash() {
        super();

        // URL url =
        // JSplash.class.getClassLoader().getResource("resources/icons/splash_logo.png");
        // 加载图标资源
        ImageIcon icon = ImageManager.getImageIconByShortName("splash_logo.png");
        // 是否启用进度条
        m_progressBar = true;
        // 是否使用信息提示
        m_progressBarMessages = true;
        // 是否使用百分比进度条
        m_progressBarPercent = false;

        //设置窗口不在最顶端
        this.setAlwaysOnTop(false);

        // build a panel with a black line for border,
        // and set it as the content pane
        //
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        // 设置黑边框
        panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setContentPane(panel);

        // build a label and set it's icon
        //

        // 带图标的标题
        JSplashLabel label = new JSplashLabel(icon, null, null, null);

        // build a progress bar
        //
        if (m_progressBar) {
            m_progress = new JProgressBar();
            // m_progress.setIndeterminate(false);
            // m_progress.setStringPainted( true );
            if (m_progressBarMessages || m_progressBarPercent) {
                m_progress.setStringPainted(true);
            } else {
                m_progress.setStringPainted(false);
            }

            if (m_progressBarMessages && !m_progressBarPercent) {
                m_progress.setString("");
            }
            // 最大值
            m_progress.setMaximum(100);
            // 最小值
            m_progress.setMinimum(0);
            // 当前值
            m_progress.setValue(0);
        }

        // add the components to the panel
        //
        getContentPane().add(label, BorderLayout.CENTER);

        if (m_progressBar) {
            getContentPane().add(m_progress, BorderLayout.SOUTH);
        }

        // validate, and display the components 显示
        pack();

        // center on screen 居中显示
        SwingUtils.centerOnScreen(this);

        // hide the panel for now... 隐藏
        setVisible(false);
    }

    /**
     * Displays the splash screen
     */
    public void splashOn() {
        setVisible(true);
    }

    /**
     * Hides and disposes the splash screen
     */
    public void splashOff() {
        setVisible(false);
        // 销毁
        dispose();
    }

    /**
     * Sets the progress indicator (values: 0 - 100).
     * <p>
     *
     * @param value The progress indicator value.
     */
    private void setProgress(int value) {
        if (m_progressBar && value >= 0 && value <= 100) {
            m_progress.setValue(value);
        }
    }

    /**
     * Sets the progress indicator (values: 0 - 100) and a label to print inside
     * the progress bar.
     * <p>
     *
     * @param value The progress indicator value.
     * @param msg   The message to print.
     */
    private void setProgress(int value, String msg) {
        setProgress(value);

        if (m_progressBarMessages && !m_progressBarPercent && msg != null) {
            m_progress.setString(msg);
        }
    }

    public void increaseProgress(int value) {
        total_progress += value;
        setProgress(total_progress);
    }

    public void increaseProgress(int value, String message) {
        System.out.println("value=" + value + "message=" + message);
        total_progress += value;
        setProgress(total_progress, message);
        //m_progress.updateUI();
    }

    public void resetProgress() {
        total_progress = 0;
        setProgress(total_progress);
    }
}
